﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/15 11:40:00
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary.Forms;
using System;
using System.Windows.Forms;

namespace CommonLibrary
{
    /// <summary>
    /// 用户管理窗体
    /// </summary>
    public partial class FormUser : HWFrmWithTitle
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FormUser()
        {
            InitializeComponent();
        }
        private void FormUser_Load(object sender, EventArgs e)
        {
            InitColumns();
            LoadUsers(); 
        }
        private void InitColumns()
        {
            CommonHelper.CreateTextGridColumn(this.dataGridViewUsers, "YGBH", "员工编号");
            CommonHelper.CreateTextGridColumn(this.dataGridViewUsers, "UserName", "用户名");
            CommonHelper.CreateTextGridColumn(this.dataGridViewUsers, "Name", "姓名");
            CommonHelper.CreateTextGridColumn(this.dataGridViewUsers, "Power", "权限");
            CommonHelper.CreateTextGridColumn(this.dataGridViewUsers, "CardNo", "卡号");
            this.dataGridViewUsers.DoubleBufferedDataGirdView(true);
            this.dataGridViewUsers.InitStyle();
        }
        private void dataGridViewTcp_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
        }
        private void dataGridViewTcp_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
        }
        private void LoadUsers()
        {
            var users = UserHelper.GetAllUsers();
            this.dataGridViewUsers.Rows.Clear();
            foreach (var user in users)
            {
                int rowIndex = this.dataGridViewUsers.Rows.Add(1);
                var row = this.dataGridViewUsers.Rows[rowIndex];
                row.Cells["YGBH"].Value = user.YGBH;
                row.Cells["UserName"].Value = user.UserName;
                row.Cells["Name"].Value = user.Name;
                row.Cells["Power"].Value = CommonHelper.GetEnumDescription(CommonHelper.GetEnumByRealValue(user.Permission, EPower.OPT));
                row.Cells["CardNo"].Value = EncryptDecryptHelper.Decrypt(user.CreditCardID);
                row.Tag = user;
            }
        }
        private void toolStripMenuItemAdd_Click(object sender, EventArgs e)
        {
            new FormUserEdit(null).ShowDialog();
            LoadUsers();
        }
        private void toolStripMenuItemEdit_Click(object sender, EventArgs e)
        {
            var selectedCells = this.dataGridViewUsers.SelectedCells;
            if (selectedCells != null && selectedCells.Count > 0)
            {
                var row = this.dataGridViewUsers.Rows[selectedCells[0].RowIndex];
                var user = (UserInfo)row.Tag;

                new FormUserEdit(user).ShowDialog();
                LoadUsers();
            }
            else
            {
                HWMessageBox.Show(this,  "请先选中记录再进行操作");
            }
        }
        private void toolStripMenuItemDelete_Click(object sender, EventArgs e)
        {
            var selectedCells = this.dataGridViewUsers.SelectedCells;
            if (selectedCells != null && selectedCells.Count > 0)
            {
                if (HWMessageBox.ShowYesNo(this, "确认删除该记录吗？") == DialogResult.Yes)
                {
                    var row = this.dataGridViewUsers.Rows[selectedCells[0].RowIndex];
                    var user = (UserInfo)row.Tag;

                    if (UserHelper.DeleteUser(user.Id))
                    {
                        HWMessageBox.Show(this,  "删除成功");
                    }
                    else
                    {
                        HWMessageBox.Show(this,  "删除失败");
                    }
                    LoadUsers();
                }
            }
            else
            {
                HWMessageBox.Show(this,  "请先选中记录再进行操作");
            }
        }
        private void dataGridViewUsers_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                var row = this.dataGridViewUsers.Rows[e.RowIndex];
                var user = (UserInfo)row.Tag;

                new FormUserEdit(user).ShowDialog();
                LoadUsers();
            }
        }
        private void FormUser_FormClosing(object sender, FormClosingEventArgs e)
        {            
        }
    }
}
